﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ListItemEditor
{
    public partial class Prompt : Form
    {
        public Prompt()
        {
            InitializeComponent();
        }

        public static DialogResult ShowPrompt(IWin32Window parent, string caption, ref string value)
        {
            return ShowPrompt(parent, caption, ref value, false);
        }

        public static DialogResult ShowPrompt (IWin32Window parent, string caption, ref string value, bool multiline)
        {
            if (string.IsNullOrEmpty (value)) value = string.Empty;
            Prompt prompt = new Prompt();
            if (multiline)
            {
                prompt.txt.AcceptsReturn = true;
                prompt.txt.Multiline = true;
                prompt.txt.ScrollBars = ScrollBars.Both;
                
                Point location = prompt.txt.Location;
                prompt.txt.Location = new Point(location.X, location.Y - 15);
                
                Size size = prompt.txt.Size;
                prompt.txt.Size = new Size(size.Width + 100, size.Height + 100);

                prompt.Size = new Size(prompt.Width + 100, prompt.Height + 100);
                prompt.btnOK.Location = new Point(prompt.btnOK.Location.X, prompt.btnOK.Location.Y + 80);
                prompt.btnCancel.Location = new Point(prompt.btnCancel.Location.X, prompt.btnCancel.Location.Y + 80);
            }

            prompt.Text = caption;
            prompt.txt.Text = value;
            DialogResult result = prompt.ShowDialog(parent);
            if (result == DialogResult.OK) value = prompt.txt.Text;
            return result;
        }

        public static DialogResult ShowMultiplePrompt(IWin32Window parent, string caption, List<string> values, List<string> labels, List<bool> passwords)
        {
            Prompt prompt = new Prompt();
            prompt.Text = caption;
            prompt.txt.Visible = false;

            int y = prompt.txt.Location.Y - 10, x = prompt.txt.Location.X, count = 0;

            prompt.SuspendLayout();

            List<TextBox> texts = new List<TextBox>();
            foreach (string s in values)
            {
                Label lbl = new Label();
                lbl.Name = "lbl" + count;
                lbl.Location = new Point(x, y);
                lbl.Size = prompt.txt.Size;
                lbl.Text = labels[count];
                prompt.Controls.Add(lbl);
                y += 20;
                
                TextBox tb = new TextBox();
                tb.Name = "txt" + count;
                tb.Text = s;
                tb.Location = new Point(x, y);
                tb.Size = prompt.txt.Size;
                if (passwords[count]) tb.PasswordChar = '*';
                prompt.Controls.Add(tb);
                y += 30;

                texts.Add(tb);
                count++;
            }

            y += 10;
            prompt.btnOK.Location = new Point(prompt.btnOK.Location.X, y);
            prompt.btnCancel.Location = new Point(prompt.btnCancel.Location.X, y);
            prompt.Size = new Size(prompt.Size.Width, prompt.Size.Height + y - 20);
            prompt.ResumeLayout(false);
            prompt.PerformLayout();

            DialogResult result = prompt.ShowDialog(parent);
            if (result == DialogResult.OK)
            {
                values.Clear(); values.AddRange(texts.Select( tb => tb.Text));
            }
            return result;
        }
    }
}